import http from '@utils/http'



<% for(let key in modules) { %>
  <% let mod = modules[key] %>
  <% let apis = Object.keys(mod) %>
  export const <%= key %> = {
    <% for(let i = 0; i < apis.length; i++) { %>
      <% let api = apis[i] %>
      <% let action = mod[api] %>
      <% let parameters = action.parameters %>
      <% let paramKeys = Object.keys(parameters) %>
      /**
       * <%= action.description %>
       *
      <% for(let i = 0; i < paramKeys.length; i++) { %>
        <% let param = parameters[paramKeys[i]]; %>
       * @param <%= paramKeys[i] %> <%= param.trim().replace(/\s+/g, ' ') %>
      <% } %>
       * @return {Promise} Any
       */
      <%= api %>: ({
        <% for(let i = 0; i < paramKeys.length; i++) { %>
          <%= paramKeys[i] %>
          <% if (i < paramKeys.length - 1) { %> , <% } %>
        <% } %>
      }) => {
        let url = `<%= action.path.replace('{{', '${').replace('}}', '}').replace('/{', '/${').replace(/\w{8}(\w{4}){3}\w{12}?/, '${id}') %>`
        return new Promise((resolve, reject) => {
          http.<%= action.method %>(url, {
            <% for(let i = 0; i < paramKeys.length; i++) { %>
              <%= paramKeys[i] %>
              <% if (i < paramKeys.length - 1) { %> , <% } %>
            <% } %>
          })
          .then(res => {
            let {status, results, token} = res
            if (status.index === '10000') {
              resolve(results)
            } else {
              reject(status)
            }
          })
          .catch(err => {
            console.log(err)
            reject(err)
          })
        })
      }
      <% if (i < apis.length - 1) { %> , <% } %>
    <% } %>
  }
<% } %>